﻿using System.Linq;
using System.Net.Http.Formatting;
using System.Web.Http;
using Newtonsoft.Json.Serialization;
using Sustainalytics.Utils.WebApi.Filters;
using System.Configuration.Abstractions;

namespace Sustainalytics.Controversies.Service
{
    static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {

            bool.TryParse(ConfigurationManager.Instance.AppSettings["enableCors"], out bool allowCors);
            if (allowCors)
                config.EnableCors();

            bool useHttps;
            bool.TryParse(ConfigurationManager.Instance.AppSettings["UseHttps"], out useHttps);

            if (useHttps)
            {
                // Force HTTPS on entire API
                config.Filters.Add(new RequireHttpsAttribute());
            }

            //use camel case for json not default pascal case from .NET
            var jsonFormatter = config.Formatters.OfType<JsonMediaTypeFormatter>().First();
            jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();

            config.Filters.Add(new CacheabilityActionFilterAttribute());
            //all routing will be done through route attributes inside controllers
            config.MapHttpAttributeRoutes();
        }
    }
}
